package com.snap.vseries.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.snap.vseries.R;
import com.snap.vseries.adapter.CitAdapter;
import com.snap.vseries.base.BaseActivity;
import com.snap.vseries.bean.SerialPortDevice;
import com.snap.vseries.bean.TestItem;
import com.snap.vseries.fragment.cit.TempCommFragment;
import com.snap.vseries.serial.SerialPortHelper;
import com.snap.vseries.utils.ToastUtils;
import com.snap.vseries.utils.TopTitleUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.snap.vseries.AppConfig.SERIAL_BAUD_RATE;
import static com.snap.vseries.AppConfig.SERIAL_PORT;


public class CitTestActivity extends BaseActivity {
    @BindView(R.id.rec)
    RecyclerView rec;

    private int currentCheck;
    private boolean isCurrentTesting = false;
    private Fragment[] fragments;
    private List<TestItem> testItems = new ArrayList<>();
    private SerialPortHelper serialPortHelper;
    private CitAdapter adapter;

    @Override
    protected int layoutResId() {
        return R.layout.activity_cit;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TopTitleUtils(this).setTitle("温控板通讯");
        serialPortHelper = new SerialPortHelper(new SerialPortDevice(SERIAL_PORT, SERIAL_BAUD_RATE));
        testItems.add(new TestItem("温控板通讯"));//结果 99%
//        testItems.add(new TestItem("采光板通讯"));//结果 99%
//        testItems.add(new TestItem("快速升降温"));//通过/未通过    也属于一种老化测试
//        testItems.add(new TestItem("热盖控温"));//通过 耗时**秒
//        testItems.add(new TestItem("风扇开关"));//通过
//        testItems.add(new TestItem("LED耐久"));//通过/未通过      也属于一种老化测试
//        testItems.add(new TestItem("电机传感器"));//通过
//        testItems.add(new TestItem("温度稳定度"));//通过
//        testItems.add(new TestItem("荧光稳定度"));//通过
        //读取版本号，生产序列号
        //生成报告 csv

        fragments = new Fragment[testItems.size()];
        testItems.get(currentCheck).setChecked(true);
        showFragment(0);

        rec.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CitAdapter(testItems, (adapter1, v, position) -> {
            if (isCurrentTesting) {
                ToastUtils.get().showText("当前测试项目尚未完成！");
                return;
            }
            if (currentCheck == position) return;
            testItems.get(currentCheck).setChecked(false);
            testItems.get(position).setChecked(true);
            adapter1.notifyItemChanged(currentCheck);
            adapter1.notifyItemChanged(position);
            showFragment(position);
        });
        rec.setAdapter(adapter);
        rec.setVisibility(View.GONE);
    }

    public void showFragment(int position) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        fragments[currentCheck] = fm.findFragmentByTag(testItems.get(currentCheck).getName());
        if (fragments[currentCheck] != null) ft.hide(fragments[currentCheck]);

        if (fragments[position] == null) {
            fragments[position] = getFragment(position);
            ft.add(R.id.container, fragments[position], testItems.get(position).getName());
        } else {
            ft.show(fragments[position]);
        }
        ft.commitAllowingStateLoss();
        currentCheck = position;
    }

    private Fragment getFragment(int position) {
        TestItem testItem = testItems.get(position);
        switch (position) {
            case 0:
                return new TempCommFragment(position, testItem);
//            case 1:
//                return new LightCommFragment(position, testItem);
//            case 2:
//                return new TempUpDowmFragment(position, testItem);
//            case 3:
//                return new LidTempFragment(position, testItem);
//            case 4:
//                return new FanFragment(position, testItem);
//            case 5:
//                return new LEDEnduranceFragment(position, testItem);
//            case 6:
//                return new MotorFragment(position, testItem);
//            case 7:
//                return new TempStabilityFragment(position, testItem);
//            case 8:
//                return new LightStabilityFragment(position, testItem);
            default:
                return null;
        }
    }

    public SerialPortHelper getSerialPortHelper() {
        return serialPortHelper;
    }

    public boolean isTesting() {
        return isCurrentTesting;
    }

    public void setTesting(boolean currentTesting) {
        isCurrentTesting = currentTesting;
    }

    public void notifyItem(int position) {
        adapter.notifyItemChanged(position);
    }
}
